package cn.com.cetccst.uploadlog;

import android.content.Context;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class UploadManager extends Thread {
    private final static String TAG = "UploadManager";

    public static final int CODE_UPLOAD_SUCCESS = 0;
    public static final int CODE_UPLOAD_AUTH_FAILED = 1;
    public static final int CODE_UPLOAD_TRANSFER_FAILED = 2;
    public static final int CODE_UPLOAD_EXCEED_LIMIT = 3;
    public static final int CODE_UPLOAD_BAD_NETWORK = 4;

    private Context mContext;
    private String mPkgName;
    private String mSrc;
    private ICALLBACK mCallback;

    public UploadManager(Context context, ICALLBACK callback) {
        this(context, context.getPackageName(), getLogSrc(context), callback);
    }

    public UploadManager(Context context, String pkgName, String src, ICALLBACK callback) {
        this.mContext = context;
        this.mPkgName = pkgName;
        this.mSrc = src;
        this.mCallback = callback;
    }

    @Override
    public void run() {
        File zipFile = zipLog();
    }

    private String getDstFullPath() {
        return mContext.getExternalFilesDir((String) null)
                + File.separator + getLogFileName();
    }

    private String getLogFileName() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH-mm-");
        return formatter.format(currentTime) + "emm.gzip";
    }

    private File copyLog(String srcFilePath, String fileName) {
        String srcPath = srcFilePath + File.separator + fileName;
        String dstPath = getLogSrc(mContext) + File.separator + fileName;
        File srcFile = new File(srcPath);
        File dstFile = new File(dstPath);
        try {
            if (dstFile.exists()) {
                dstFile.delete();
            }
            if (srcFile.exists()) {
                FileUtils.copyFileUsingFileChannels(srcFile, dstFile);
            }
        } catch (IOException e) {
            Logger.log(Log.ERROR, TAG, "copyLog path: " + srcFilePath, e);
        }
        return dstFile;
    }

    public File zipLog() {
        File src = new File(mSrc);
        File dst = new File(getDstFullPath());
        if (dst.exists()) {
            dst.delete();
        }

//        copyEcmLog();

        if (src.isDirectory()) {
            FileUtils.gzipFolder(src, dst);
        } else {
            FileUtils.gzipFile(src, dst);
        }

        File[] dstFilesNeedDlt = dst.listFiles();
        if (null != dstFilesNeedDlt) {
            for (File fileInDst : dstFilesNeedDlt) {
                fileInDst.delete();
            }
        }

        return dst;
    }

    public static void successCallback(ICALLBACK callback) {
        Logger.log(Log.INFO, TAG, "successCallback");
        if (callback != null) {
            callback.callback(CODE_UPLOAD_SUCCESS, "success");
        }
    }

    public static void failCallback(ICALLBACK callback, int code, String msg) {
        Logger.log(Log.INFO, TAG, "failCallback " + msg);
        if (callback != null) {
            callback.callback(code, msg);
        }
    }

    public static void exceedFailed(ICALLBACK callback) {
        failCallback(callback, CODE_UPLOAD_EXCEED_LIMIT, "File size Exceed the upper limit");
    }

    public static void authFailed(ICALLBACK callback) {
        failCallback(callback, CODE_UPLOAD_AUTH_FAILED, "invalid token, please retry");
    }

    public static void transferFailed(ICALLBACK callback) {
        failCallback(callback, CODE_UPLOAD_TRANSFER_FAILED, "recv file failed, please retry");
    }

    public static void badNetworkFailed(ICALLBACK callback) {
        failCallback(callback, CODE_UPLOAD_BAD_NETWORK, "bad network, please retry");
    }

    public static String getLogSrc(Context context) {
        return context.getExternalFilesDir((String) null)
                + File.separator + "logs";
    }

}
